package com.sunlong.jiangwei.service.impl;

import com.sunlong.jiangwei.dao.AsideMenuMapper;
import com.sunlong.jiangwei.dao.AsideMenuSMapper;
import com.sunlong.jiangwei.entity.AsideMenu;
import com.sunlong.jiangwei.entity.AsideMenuS;
import com.sunlong.jiangwei.service.IAsideMenu;
import com.sunlong.jiangwei.service.IAsideMenuS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class AsideMenuSImpl implements IAsideMenuS {
    @Autowired
    private AsideMenuSMapper asideMenuMapper;
    @Override
    public List<AsideMenuS> selectMenuItemsAll() {
        List<AsideMenuS> byParent = getAsideMenusByParent(0);
        for (int i = 0; i < byParent.size(); i++) {
            AsideMenuS asideMenu = byParent.get(i);
            List<AsideMenuS> byParent1 = getAsideMenusByParent(asideMenu.getId());
            asideMenu.setChildren(byParent1);
        }
        return byParent;
    }

    private List<AsideMenuS> getAsideMenusByParent(int parent) {
        Example example = new Example(AsideMenuS.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parent",parent);
        return asideMenuMapper.selectByExample(example);
    }
}
